package com.naiterui.ehp.bs.b2c.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.naiterui.ehp.bs.b2c.product.entity.Classification;
import com.naiterui.ehp.bs.b2c.product.entity.Medicine;
import com.naiterui.ehp.bs.b2c.product.entity.Product;
import com.naiterui.ehp.bs.b2c.product.entity.Sku;
import com.naiterui.ehp.bs.b2c.product.repository.IClassificationRepository;
import com.naiterui.ehp.bs.b2c.product.repository.IMedicineRepository;
import com.naiterui.ehp.bs.b2c.product.repository.IProductRepository;
import com.naiterui.ehp.bs.b2c.product.repository.ISkuRepository;
import com.naiterui.ehp.bs.b2c.product.service.IB2cSuperviseService;
import com.naiterui.ehp.bs.b2c.product.vo.SuperviseSkuVO;
import com.naiterui.ehp.bs.b2c.product.vo.TianjinSuperviseProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <pre>
 *
 * </pre>
 *
 * @author fangguanhong
 * @date 2021/1/14 下午5:36
 * @since 1.0.0
 */
@Service
public class B2cSuperviseServiceImpl implements IB2cSuperviseService {

    @Autowired
    private ISkuRepository skuRepository;
    @Autowired
    private IProductRepository productRepository;
    @Autowired
    private IClassificationRepository classificationRepository;
    @Autowired
    private IMedicineRepository medicineRepository;

    @Override
    public List<SuperviseSkuVO> recomProduct(List<Long> skuIds) {
        List<SuperviseSkuVO> result = new ArrayList<>();

        List<Sku> skus = skuRepository.findAllByIdIn(skuIds);
        List<Long> productIds = skus.stream().map(sku -> sku.getProductId()).collect(Collectors.toList());
        List<Product> products = productRepository.findAllByIdIn(productIds);
        // 批准文号列表
        List<String> approvalNumbers = products.stream().map(product -> product.getApprovalNumber()).collect(Collectors.toList());

        List<Medicine> medicines = medicineRepository.findByProductIdIn(productIds);
        Map<Long, Medicine> medicineMap = medicines.stream().collect(Collectors.toMap(Medicine::getProductId, e -> e));

        Map<Long, Product> productMap = products.stream().collect(Collectors.toMap(Product::getId, e -> e));
        List<Long> classificationIds = products.stream().map(product -> product.getClassificationId()).collect(Collectors.toList());
        List<Classification> classifications = classificationRepository.findByIdIn(classificationIds);
        Map<Long, Classification> classificationMap = classifications.stream().collect(Collectors.toMap(Classification::getId, e -> e));

        if (CollectionUtil.isNotEmpty(skus)) {
            for (Sku sku : skus) {
                {
                    SuperviseSkuVO vo =
                            SuperviseSkuVO.builder()
                                          //.id(sku.getId().toString())
                                          .skuId(sku.getId())
                                          .spec(sku.getPackingSpec())
                                          .build();
                    Product product = productMap.get(sku.getProductId());

                    Medicine medicine = medicineMap.get(sku.getProductId());
                    vo.setPackingUnit(medicine.getPackingUnit());

                    if (null != classificationMap && null != product
                            .getClassificationId() && null != classificationMap.get(product
                            .getClassificationId())) {
                        Classification classification = classificationMap.get(product.getClassificationId());
                        vo.setPillCode(classification.getCode());
                        vo.setPillName(classification.getName());
                    }
                    result.add(vo);
                }
            }
        }

        return result;
    }

    @Override
    public List<TianjinSuperviseProductVO> tianjinProduct(Date startDate, Date endDate) {
        List<Medicine> medicineList = medicineRepository.findByCreatedAtBetween(startDate, endDate);
        if (CollectionUtil.isEmpty(medicineList)) {
            return null;
        }
        Set<Long> productIds = medicineList.stream().map(Medicine::getProductId).collect(Collectors.toSet());
        List<Product> products = productRepository.findAllByIdIn(productIds);
        Map<Long, Product> productMap = products.stream().collect(Collectors.toMap(Product::getId, p -> p));
        List<TianjinSuperviseProductVO> result = new ArrayList<>();
        for (Medicine medicine : medicineList) {
            Product product = productMap.get(medicine.getProductId());
            result.add(TianjinSuperviseProductVO.builder()
                    .productId(product.getId())
                    .drugClassificationId(medicine.getDrugClassificationId())
                    .productName(product.getName())
                    .commonName(medicine.getCommonName())
                    .specification(medicine.getSpecification())
                    .productionEnterprise(medicine.getProductionEnterprise())
                    .valid(product.getValid())
                    .build());
        }
        return result;
    }
}
